package com.nlp.visualization.service.impl.weibo;

import com.nlp.visualization.dao.weibo.IWeiboUserHash;
import com.nlp.visualization.pojo.weibo.TWeiboUserHash;
import com.nlp.visualization.service.weibo_mine.IHashService;
import com.nlp.visualization.service.weibo_mine.IWeiboUserHashService;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author LXM
 * @Title: HashServiceImpl
 * @Description: Hash服务实现类
 * @date 2018/3/17上午11:39
 */

public class HashServiceImpl implements IHashService {

    Logger logger = LoggerFactory.getLogger(HashServiceImpl.class);

    @Autowired
    static IWeiboUserHashService weiboUserHashService;

    static Map<String, String> tableMap = new HashedMap();

    static {

        List<TWeiboUserHash> hashes = weiboUserHashService.findAll();
        for (TWeiboUserHash h : hashes) {
            tableMap.put(h.getHashValue(), h.getTableName());
        }

    }

    @Override
    public String hashByName(String name) {

        String y = "";
        long x = 0;
        try {
            x = Long.parseLong(name) % 3;
            logger.info("用户:" + name + " ，取得Hash值：" + x);
        } catch (Exception e) {
            logger.error("Hash求余失败");
            e.printStackTrace();
        }
        return tableMap.get(x + "");
    }
}
